const app = getApp()
var api = require('../../utils/api')
// pages/home/index.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    TabCur: 0, // 门诊业务 住院业务 便民服务
    gridCol: 4,
    commonCol: 3,
    sideBarstart: 0, //触摸屏幕初始位置
    // 轮播图
    swiperList: [{
      id: 0,
      type: 'image',
      url: '/images/nav/nav1.jpg'
    }, {
      id: 1,
      type: 'image',
      url: '/images/nav/nav2.jpg',
    }, {
      id: 2,
      type: 'image',
      url: '/images/nav/nav3.jpg'
    }, {
      id: 3,
      type: 'image',
      url: '/images/nav/nav4.jpg'
    }, {
      id: 4,
      type: 'image',
      url: '/images/nav/nav5.jpg'
    }, {
      id: 5,
      type: 'image',
      url: '/images/nav/nav6.jpg'
    }],
    // 常用功能
    commonList: [{
      icon: 'icon-order',
      color: 'red',
      name: '预约挂号'
    }, {
      icon: 'icon-menzhenjiaofei',
      color: 'orange',
      name: '门诊缴费'
    }, {
      icon: 'icon-jiuzhenqia',
      color: 'cyan',
      name: '就诊卡管理'
    }],
    // 功能分类
    funcTypeList: [{
      icon: 'apps',
      name: '门诊业务',
      id: 0
    },{
      icon: 'apps',
      name: '住院业务',
      id: 1
    },{
      icon: 'apps',
      name: '便民服务',
      id: 2
    }],
    // 界面上选中得功能列表
    selectFuncList:[],
    // 功能列表汇总
    iconList: [{
      icon: 'icon-tubiaozhizuomobankuozhan-',
      color: 'red',
      name: '挂号记录',
      parentId: 0,
      id: 4
    }, {
      icon: 'icon-jianyandan',
      color: 'orange',
      name: '检验查询',
      parentId: 0,
      id: 5
    }, {
      icon: 'icon-jiancha',
      color: 'yellow',
      name: '检查查询',
      parentId: 0,
      id: 6
    }, {
      icon: 'icon-yijiyuyue',
      color: 'olive',
      name: '医技预约',
      parentId: 0,
      id: 7
    }, {
      icon: 'icon-chuzhilishi',
      color: 'cyan',
      name: '治疗预约',
      parentId: 0,
      id: 8
    }, {
      icon: 'icon-clock',
      color: 'blue',
      name: '候诊查询',
      parentId: 0,
      id: 9
    }, {
      icon: 'icon-yuyue',
      color: 'purple',
      name: '预约记录',
      parentId: 0,
      id: 10
    }, {
      icon: 'icon-paiban',
      color: 'green',
      name: '排班查询',
      parentId: 0,
      id: 11
    }, {
      icon: 'icon-qingdan',
      color: 'pink',
      name: '住院费用清单',
      parentId: 1,
      id: 12
    }, {
      icon: 'icon-tubiaolunkuo_huaban',
      color: 'brown',
      name: '住院申请',
      parentId: 1,
      id: 13
    }, {
      icon: 'icon-zhuyuanyajin',
      color: 'rgb(16,162,212)',
      name: '住院押金',
      parentId: 1,
      id: 14
    }, {
      icon: 'icon-1',
      color: 'rgb(34,168,255)',
      name: '缴费记录',
      parentId: 1,
      id: 15
    }, {
      icon: 'icon-navicon-wgxzsz',
      color: 'rgb(130,216,66)',
      name: '住院须知',
      parentId: 1,
      id: 16
    }, {
      icon: 'icon-daohang',
      color: 'rgb(230,46,40)',
      name: '院内导航',
      parentId: 2,
      id: 17
    }, {
      icon: 'icon-zhinan',
      color: 'rgb(71,179,71)',
      name: '就诊指南',
      parentId: 2,
      id: 18
    }, {
      icon: 'icon-swticondingcan',
      color: 'rgb(209,82,147)',
      name: '住院订餐',
      parentId: 2,
      id: 19
    }, {
      icon: 'icon-shouye',
      color: 'rgb(64,203,98)',
      name: '病历复印',
      parentId: 2,
      id: 20
    }, {
      icon: 'icon-tingche',
      color: 'rgb(234,59,106)',
      name: '智慧停车',
      parentId: 2,
      id: 21
    }]
  },
  /**
   * 组件的方法列表
   */
  methods: {
    NavChange(e) {
      console.log(e.currentTarget.dataset.cur)
      this.setData({
        PageCur: e.currentTarget.dataset.cur
      })
    },// 获取当前的tab功能页
    getCurrentPageFunc(id){
      return this.data.iconList.filter(m=>m.parentId===id);
    },
    // tab页选择事件
    tabSelect(e) {
      // var array = this.getCurrentPageFunc(e.currentTarget.dataset.id)
      // this.setData({
      //   TabCur: e.currentTarget.dataset.id,
      //   selectFuncList: array
      // })
      this.changePage(e.currentTarget.dataset.id);
    },
    pageTouchStartHandler: function (e) {
      this.data.sideBarstart = e.changedTouches[0].pageX
    },
    pageTouchEndHandler: function (e) {
      var touchend = e.changedTouches[0].pageX;
      var id =  this.data.TabCur;  
      // 右滑
      if (touchend - this.data.sideBarstart < -50) {
        if(id == 2){
          return;
        }
        this.changePage(id + 1)
      }
      // 左滑
      if ( touchend - this.data.sideBarstart > 50) {
        if(id == 0){
          return;
        }
        this.changePage(id - 1)
      }
    },
    handleFuncListClick:function(e){
      let id = e.currentTarget.dataset.id
      if("5"==id){//检验查询
        wx.navigateTo({
          url: '/pages/clinicQuery/clinicQuery?id=0',
        })
      }else if("6"==id){//检查查询
        wx.navigateTo({
          url: '/pages/clinicQuery/clinicQuery?id=1',
        })
      }else if("4"==id){//挂号记录
        wx.navigateTo({
          url: '/pages/register/registerAndAppiointHistory?id=0',
        })
      }else if("10"==id){
        wx.navigateTo({
          url: '/pages/register/registerAndAppiointHistory?id=1',
        })
      }else{
        wx.showToast({
          title: '该功能还在开发中',
          icon: 'none'
        })
      }
    },
    changePage(id){
      var array = this.getCurrentPageFunc(id)
      this.setData({
        TabCur: id,
        selectFuncList: array
      })
    },
    handleHospital(){
      wx.navigateTo({
        url: '/pages/hospital/hospital'
      })
    },
    handleDept(){
      wx.navigateTo({
        url: '/pages/dept/dept'
      })
    },
    handleDoctor(){
      wx.navigateTo({
        url: '/pages/doctor/doctor'
      })
    },
    handleLine(){
      wx.navigateTo({
        url: '/pages/line/line'
      })
    },
    /**
     * 预约挂号
     * @param {事件对象} event 
     */
    handleAppiointReg(event){
      if("预约挂号"===event.currentTarget.dataset.name){
        wx.navigateTo({
          url: '/pages/appiointChoose/deptAndDoctorChoose'
        })
      } else if("门诊缴费"===event.currentTarget.dataset.name){
        wx.navigateTo({
          url: '/pages/charge/chargeQuery',
        })
      } else if("就诊卡管理"===event.currentTarget.dataset.name){
        wx.navigateTo({
          url: '/pages/user/cardManagement'
        })
      }
    }
  },
  async ready() {
    var array = this.getCurrentPageFunc(this.data.TabCur);
    // 通过接口获取系统参数
    await api.getHosConfig("100001").then(res => {
      app.globalData.hospital.name = res[0].sysStr
    })
    this.setData({
      selectFuncList: array,
      hispitalName: app.globalData.hospital.name
    });
  },
   options: {
    addGlobalClass: true
  }
})
